﻿using AdminManager.Permissions;
using AdminManager.System.AuditLogs;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.AuditLogging;

namespace AdminManager.System
{
    [Authorize(AdminManagerPermissions.AuditLogs.Default)]
    public class AuditLogsAppService : ApplicationService, IAuditLogsAppService
    {
        private readonly IAuditLogRepository _auditLogRepository;

        public AuditLogsAppService(IAuditLogRepository auditLogRepository)
        {
            this._auditLogRepository = auditLogRepository;
        }
        public async Task<PagedResultDto<AuditLogsDto>> GetListAsync(AuditLogsSearchDto auditLogsInputDto)
        {

            var list = await this._auditLogRepository.GetListAsync(auditLogsInputDto.Sorting,
                auditLogsInputDto.MaxResultCount, auditLogsInputDto.SkipCount-1, auditLogsInputDto.startTime,
                auditLogsInputDto.endTime, auditLogsInputDto.httpMethod, auditLogsInputDto.url,
                auditLogsInputDto.userId, auditLogsInputDto.userName, auditLogsInputDto.applicationName,
                auditLogsInputDto.clientIpAddress);
            var totalCount = await this._auditLogRepository.GetCountAsync(auditLogsInputDto.startTime,
                auditLogsInputDto.endTime, auditLogsInputDto.httpMethod, auditLogsInputDto.url, auditLogsInputDto.userId, auditLogsInputDto.userName,
                auditLogsInputDto.applicationName, auditLogsInputDto.clientIpAddress);
            return new PagedResultDto<AuditLogsDto>(totalCount, ObjectMapper.Map<List<AuditLog>, List<AuditLogsDto>>(list));
        }
    }
}
